using MediatR;
using Microsoft.AspNetCore.Mvc;
using WMS.ErrorCode;
using WMS.WebApiRead.Application.Command;
using WMS.WebApiRead.DTO;

namespace WMS.WebApiRead.Controllers
{
    /// <summary>
    /// 盘点信息读操作控制器（查询）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InventoryController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<InventoryController> logger;

        /// <summary>
        /// 构造函数，注入MediatR和日志服务
        /// </summary>
        public InventoryController(IMediator mediator, ILogger<InventoryController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 查询盘点信息列表（多表联查+分页）
        /// 联查：盘点表、盘点明细表、上传附件表、经办人信息表、仓库表、库管部门表
        /// </summary>
        /// <param name="command">查询命令</param>
        /// <returns>返回分页的盘点信息列表</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<List<InventoryQueryResultDTO>>>> QueryInventoryList([FromQuery] InventoryQueryCommand command)
        {
            logger.LogInformation($"开始查询盘点信息列表，" +
                                $"页码：{command.QueryDTO.PageIndex}，" +
                                $"每页大小：{command.QueryDTO.PageSize}，" +
                                $"盘点单号：{command.QueryDTO.InventoryCode ?? "全部"}");

            var result = await mediator.Send(command);

            logger.LogInformation($"查询盘点信息列表完成，" +
                                $"总数：{result.Data?.TotalCount ?? 0}，" +
                                $"总页数：{result.Data?.TotalPage ?? 0}");

            return result;
        }

        /// <summary>
        /// 查询盘点信息详情（包含主表信息、明细列表、附件列表）
        /// 多表联查：盘点表、仓库表、经办人信息表、库管部门表、盘点类型表
        /// </summary>
        /// <param name="command">查询详情命令</param>
        /// <returns>返回盘点详情（包含明细和附件）</returns>
        [HttpGet]
        public async Task<ApiResult<InventoryDetailResultDTO>> GetInventoryDetail([FromQuery] InventoryDetailQueryCommand command)
        {
            logger.LogInformation($"开始查询盘点详情，盘点编号：{command.InventoryCode}");

            var result = await mediator.Send(command);

            logger.LogInformation($"查询盘点详情完成，结果：{result.Message}");

            return result;
        }
    }
}

